<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2018/11/21
 * Time: 10:49 AM
 */

namespace Biz\module\Decoupling\order;

use Biz\module\Classes\OrderSms as clsOrderSms;
use Biz\module\Decoupling\AbstractMain;
use Biz\service\Event;
use Doraemon\tools\Log as tLog;
use Biz\model\trade\OrderOperateLog as mOrderOperateLog;
use Biz\model\trade\OrderGoods as mOrderGoods;
use Doraemon\amqp\Queue as aQueue;
use Biz\model\OperateLog as mOperateLog;
use Biz\service\trade\Pay;
use Biz\module\Classes\OrderRefund as clsOrderRefund;
use Biz\service\OperateLog as sOperateLog;
use Biz\service\ShopInfo;
use Biz\service\TradeMsg as sTradeMsg;
use Biz\model\trade\Order as mOrder;
use Biz\model\trade\OrderRefund as mOrderRefund;
use Biz\model\trade\ShopCourier as mShopCourier;

class ShipOrder extends AbstractMain
{
    const NAME = '发货事件回调';

    public function main($message, $data)
    {

        //解析 json 并赋值变量
        $appendData = [
            'shopId' => $data['body']['shop_id'],
            'orderNo' => $data['body']['order_no'],
            'addTime' => $data['header']['addtime'],
            'skuIds' => $data['body']['sku_ids'],
            'courierId'=>$data['body']['courier_id'],
            'expressData' => [
                'express_company' => $data['body']['express_company'],
                'express_no' => $data['body']['express_no']
            ],
            'isPackageDelivery' => $data['body']['is_package_delivery'],
            'isMulti' => $data['body']['is_multi'],
            'isDeliveryAll' => $data['body']['is_delivery_all'],
            'packageId' => $data['body']['package_id'],
            'whereSkuIds' => [
                'sku_id' => $data['body']['sku_ids']
            ],
            'refundingStatus' => $data['body']['refunding_status']
        ];

        $appendData['orderInfo'] = mOrder::getOrderInfo($appendData['orderNo']);

        $appendData['firstSkuInfo'] = mOrderGoods::getFirstSkuInfo($appendData['orderNo'], $appendData['whereSkuIds']);


        $this->appendData($appendData);


    }

    protected function _exec_order_operate_log()
    {
        //交易记录日志
        mOrderOperateLog::createLog($this->data['shopId'], $this->data['orderNo'], mOrderOperateLog::TRADE_TYPE_ORDER, [
            'operate_user_type' => mOrderOperateLog::OPERATE_USERTYPE_MGT,
            'operate_uid' => $this->data['shopId'],
            'operate_type' => mOrderOperateLog::OPERATE_TYPE_SHIP_ORDER
        ], '', $this->data['isMulti'] ? '商家批量发货' : ($this->data['packageId'] > 0 ? '商家分单发货' : '商家发货'));

        $this->appendLog('交易记录日志', $this->data);
    }

    protected function _exec_send_trade_msg()
    {
        //给买家发送系统通知
        $msgData = [
            'express_company' => $this->data['expressData']['express_company'],
            'express_no' => $this->data['expressData']['express_no'],
            'order_no' => $this->data['orderNo'],
        ];


        if (isset($this->data['refundingStatus']) && $this->data['refundingStatus'] == clsOrderRefund::REFUNDING_YES) {
            $msgCode = 102;

            //如果是有处理中的退款申请，则将退款申请的状态设置为退款取消

            $refundSkuInfo = mOrderRefund::getFirstRefundingSkuInfo($this->data['orderNo'], $this->data['whereSkuIds']);


            $msgData['img'] = (isset($refundSkuInfo) && !empty($refundSkuInfo)) ? $refundSkuInfo['cover_img_thumb_url'] : '';
        } else {
            $msgCode = 101;

            $msgData['img'] = $this->data['firstSkuInfo']['cover_img_thumb_url'];

            $msgData['desc'] = $this->data['firstSkuInfo']['title'];
        }

        if (!in_array($this->data['orderInfo']['from_source'], [mOrder::FROM_SOURCE_PARAGRAPH_MINIPROGRAM])) {

            sTradeMsg::addMsg($msgCode, $this->data['orderInfo']['uid'], $msgData);

            $this->appendLog('交易系统通知', array_merge($this->data, ['msg_data' => $msgData]));

        }
    }

    protected function _exec_send_mini_program_msg()
    {

        if ($this->data['orderInfo']['pay_way'] == Pay::_PAY_WAYS_MINIPROGRAM) {
            $shopInfo = ShopInfo::getShopInfo($this->data['shopId'], []);
            $noticeData = [
                'keyword1' => (isset($this->data['firstSkuInfo']) && !empty($this->data['firstSkuInfo'])) ? $this->data['firstSkuInfo']['title'] : '',
                'keyword2' => $shopInfo['shop_name'],
                'keyword3' => $this->data['orderNo'],
                'keyword4' => $this->data['orderInfo']['pay_amount'],
                'keyword5' => $this->data['expressData']['express_company'],
                'keyword6' => $this->data['expressData']['express_no'],
                'keyword7' => '卖家已发货，请注意查收'
            ];
            sTradeMsg::sendMiniProgramShipNotice($this->data['orderNo'], $noticeData);

            $this->appendLog('小程序交易通知', array_merge($this->data, ['notice_data' => $noticeData]));

        }
    }

    protected function _exec_send_jiexuan_mini_program_msg()
    {
        //如果是店铺自提的发货，则不需要发通知
        if($this->data['courierId'] == mShopCourier::SELF_LIFTING_ID){
            return true;
        }


        if ($this->data['orderInfo']['from_source'] == mOrder::FROM_SOURCE_PARAGRAPH_MINIPROGRAM) {
            $tip = count($this->data['skuIds']) > 1 ? ' 等' : '';
            $noticeData = [
                'keyword1' => (isset($this->data['firstSkuInfo']) && !empty($this->data['firstSkuInfo'])) ? $this->data['firstSkuInfo']['title'] . $tip : '',
                'keyword2' => $this->data['expressData']['express_company'],
                'keyword3' => $this->data['expressData']['express_no'],
                'keyword4' => $this->data['orderInfo']['consignee_address'],
                'keyword5' => $this->data['addTime'],
                'keyword6' => $this->data['orderNo'],
            ];
            sTradeMsg::sendJieXuanProgramShipNotice($this->data['orderNo'], $noticeData);

            $this->appendLog('节选小程序通知', array_merge($this->data, ['notice_data' => $noticeData]));

        }
    }


    protected function _exec_operate_log()
    {
        $addTime = date('Y-m-d H:i:s');
        //日志
        sOperateLog::record(mOperateLog::TYPE_ORDER_MGT_CHANGE_ORDERS_STATUS, $this->data['shopId'], $this->data['orderNo'], $opLog = [
            'uid' => $this->data['orderInfo']['uid'],
            'express_no' => $this->data['expressData']['express_no'],
            'express_company' => $this->data['expressData']['express_company'],
            'is_package_delivery' => $this->data['isPackageDelivery'],
            'package_id' => $this->data['packageId'],
            'status' => $this->data['orderInfo']['status'],
            'delivery_time' => $this->data['addTime']
        ], [],0,$addTime);

        $this->appendLog('operatelog记录', array_merge($this->data, ['op_log' => $opLog]));
    }

    protected function _exec_paragraph_ship_notice(){
        //如果是店铺自提的发货，则不需要发通知
        if($this->data['courierId'] == mShopCourier::SELF_LIFTING_ID){
            return true;
        }

        if ($this->data['orderInfo']['pay_way'] == Pay::_PAY_WAYS_THE_PARAGRAPH_MINIPROGRAM) {
            $skuAllIds = mOrderGoods::getOrderGoodsList($this->data['orderNo']);

            clsOrderSms::noticeParagraphShipOrder($this->data['orderNo'],$this->data['orderInfo']['consignee_phone'],count($skuAllIds),$this->data['packageId'],$this->data['expressData']);
        }
    }
}